<?php

namespace app\store\controller\shops;

use app\store\controller\BaseController;
use app\store\model\PickPoint;
use app\store\service\PickService;
use think\exception\ValidateException;
use think\facade\Request;
use think\facade\View;

/**
 * 自提点
 */
class PickController extends BaseController
{
    public function index()
    {
        $param = $this->request->param();
        $pickService = new PickService();
        $list = $pickService->findAll();
        // print_r($list);exit;
        return View::fetch('', ['list' => $list]);
    }

    /**
     * 删除
     */
    public function delete()
    {
        $id = $this->request->get('id/d');
        $result = PickPoint::destroy($id);

        return $result ? json(['code' => 1, 'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败!']);
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id = $this->request->get('id/d');
        $pickService = new PickService();
        $row = $pickService->findOne($id);
        if (empty($row['addr'])) {
            $row['addr'] = $row['province'] . $row['city'] . $row['area'] . $row['address'];
        }
        if ($this->request->isPost()) {
            $param = $this->request->post();
            $id = $param['id'];
            $param = Request::only(['name', 'contact_phone', 'lat', 'lng', 'contact_name']);
            try {
                validate('PickValidate')->check($param);
            } catch (ValidateException $e) {
                return json(['code' => 0, 'msg' => $e->getMessage()]);
            }
            // print_r($param);exit;
            $rest = $pickService->update($param, $id);
            if ($rest) {
                return json(['code' => 1, 'msg' => '编辑成功']);
            } else {
                return json(['code' => 0, 'msg' => '编辑失败']);
            }
        }

        return View::fetch('', ['row' => $row]);
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->post();
            $param = Request::only(['name', 'contact_phone', 'lat', 'lng', 'contact_name']);
            try {
                validate('PickValidate')->check($param);
            } catch (ValidateException $e) {
                return json(['code' => 0, 'msg' => $e->getMessage()]);
            }
            $pickService = new PickService();
            $param['store_id'] = session('store_id');
            $rest = $pickService->create($param);
            if ($rest) {
                return json(['code' => 1, 'msg' => '添加成功']);
            } else {
                return json(['code' => 0, 'msg' => '添加失败']);
            }
        }
        return View::fetch();
    }

    // public function list()
    // {

    //     $param = $this->request->param();

    //     $storeUserService = new OfflineShopService();
    //     $list = $storeUserService->shopList($param);
    //     // print_r($list);exit;
    //     return View::fetch('',['list' => $list]);
    // }

    // public function option()
    // {
    //     $param = $this->request->param();

    //     $storeUserService = new UserService();
    //     $list = $storeUserService->getUserList($param);

    //     View::engine()->layout(false);

    //     return View::fetch('',['list' => $list]);
    // }

    // public function edit()
    // {
    //     $id = $this->request->param('id/d');
    //     $StoreUserService = new OfflineShopService();
    //     $row = $StoreUserService->findOne($id);
    //     return View::fetch('',['row' => $row]);
    // }

    // public function update()
    // {
    //     $param = $this->request->param();
    //     $id = $this->request->param('id/d');
    //     $StoreUserService = new OfflineShopService();
    //     $row = $StoreUserService->findOne($id);
    //     if(empty($row) ) {
    //         return json(['code' => 0, 'msg' => '未找到指定内容!']);
    //     }
    //     if ($row['status'] !=1) {
    //         return json(['code' => 0, 'msg' => '未找到指定内容!']);
    //     }
    //     if (isset($param['status']) && $param['status']) {
    //         $rst = $StoreUserService->auditShop($param['status'], $id, $row['uid']);
    //         if (!$rst) {
    //             return json(['code' => 0, 'msg' => '审核是吧!']);
    //         }
    //     }
    //     return json(['code' => 1, 'msg' => '操作成功!']);
    // }

}
